<?php
            
use app\model\Util; 
use app\model\Theme; 


/**
 * Desc:
 * Time: 2021/5/28 2:13 下午
 */
class Flag_Page extends PluginWebPage {

    function main($method)
    {
        $method = 'flag_main';
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 10;
        $page_start = ($page_index-1)*$page_size;


        $lists = LingzeModel::getDefaultLtList($themeid,0,0,0);
        $list = LingzeModel::getDefaultLtList($themeid,0,$page_start,$page_size);
        $total = $page_total = count($lists);

        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/lingze/flag/main', $return_clicent);
    }

    function search()
    {
        $method = 'flag_main';
        global $_G,$_P,$_S,$_C;
        $themeid = $_P['themeid'];
        $page_index = max(1,intval($_P['page']));
        $page_size = 10;
        $page_start = ($page_index-1)*$page_size;

        if (empty($_P['createtime']['start'])){
            $start_day =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
            $start_day =  date('Y-m-d',$start_day);
        }else{
            $start_day =  $_P['createtime']['start'];
        }
        $end_day = empty($_P['createtime']['end']) ? date('Y-m-d',TIMESTAMP): $_P['createtime']['end'] ;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $lists = LingzeModel::searchDefaultLt($themeid,$search_data,0,0,$start_day,$end_day);
        $total = count($lists);

        if(checksubmit('export_submit', true)){
            $count = $total;
            $page_limit = 2000;
            $pagesize = ceil($count/$page_limit);
            $header = array(
                'uid' => '会员UID',  'mobile' => '手机',
                'nickname' => '微信昵称','chest_name' => '奖品名称','level_sort'=>'通过关卡', 'box_name' => '宝箱名称',
                'created_at'=>'时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";
//            for ($j = 1; $j <= $pagesize; $j++) {

               $ex_page = $_P['ex_page']?$_P['ex_page']:1;
                $condition = "  ";
                if ($search_data){
                    $condition .= " AND b.nickname LIKE '".$search_data."'"." or b.mobile LIKE '".$search_data."' "." or c.chest_name LIKE '".$search_data."' ".
                        " or c.box_name LIKE '".$search_data."' ";
                }
                if ($start_day && $end_day && $start_day!=$end_day){
                    $condition .= "  AND a.created_at between '".$start_day."' and '".$end_day."'";
                }
                $condition1 = "  order by a.created_at desc limit ". ($ex_page - 1) * $page_limit . ",$page_limit ";

                $sql = " SELECT a.*,b.avatar,b.nickname,b.username,b.mobile,c.chest_name,c.box_name,c.level_sort,c.small_img FROM ".tablename(yx_lz_flag)." AS  a ".
                    " LEFT JOIN ".tablename(t_fans)." AS b ON a.uid=b.uid and b.themeid=$themeid".
                    " LEFT JOIN ".tablename(yx_lz_prize)." AS c ON a.lc_id=c.lc_id and c.themeid=$themeid".
                    " WHERE a.themeid=$themeid ".$condition.$condition1;
                $excel_list = db_select($sql);
                if (!empty($excel_list)) {
                    $size = ceil(count($excel_list) / 500);
                    for ($i = 0; $i < $size; $i++) {
                        $buffer = array_slice($excel_list, $i * 500, 500);
                        foreach ($buffer as $row) {
                            $row['level_sort'] = '第'.$row['level_sort'].'关';
                            unset($info);
                            if ($row['nickname']){
                                $row['nickname'] = str_replace(',','',$row['nickname']);
                                $row['nickname'] = str_replace('"','',$row['nickname']);
                            }
                            foreach ($keys as $key) {
                                $data[] = $row[$key];
                            }
                            $user[] = implode("\t ,", $data) . "\t ,";
                            unset($data);
                        }
                        $html .= implode("\n", $user)."\n";
                        unset($user);
                    }
              //  }
            }
            //新版本导出合并
            $is_end = 0;
            if($ex_page>=$pagesize){
                $is_end = 1;
            }
            Util::WxJump($_S,$html,'领泽新营销-新宝箱中奖记录',$themeid,$is_end,$pagesize);
        }

        $list = LingzeModel::searchDefaultLt($themeid,$search_data,$page_start,$page_size,$start_day,$end_day);
        $page_total = $total;
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/lingze/flag/main', $return_clicent);
    }
}